// miniprogram/pages/orderDetail/orderDetail.js
import {orderDetail, cancelAcceptOrder_, collectOrder_, cancelOrder_, confirmOrder_, cancelConfirmOrder_} from '../../apis/order'
import {authInfo} from '../../apis/user'
const util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    state: '',
    stateName: '',
    collectorName: '',
    collectorPhone: '',
    employerName: '',
    employerPhone: '',
    amount: '',
    totalWeight: '',
    address: "",
    remark: "",
    subscriptionTime: "",
    acceptTime: '',
    createTime: '',
    details: [],
    space: "",
    cancel: false,
    collect: false,
    cancelAccept: false,
    confirm: false,
    curUserId: '',
    detailValue: '',
    longitude: '',
    latitude: ''
  },

  toNavigation() {
    wx.openLocation({
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      scale: 18
    })
  },
  cancelOrder() {
    cancelOrder_(this.data.id).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: '取消成功',
        })
        this.setData({
          cancel: false
        })
        this.init();
      } else {
        wx.showToast({
          title: res.message,
        })
      }
    })
  },

  confirmOrder() {
    confirmOrder_(this.data.id).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: '确认成功',
          content: '可前往[我的钱包]查看详情'
        })
        this.setData({
          confirm: false
        })
        this.init();
      } else {
        wx.showToast({
          title: res.message,
        })
      }
    })
  },

  cancelConfirmOrder() {
    cancelConfirmOrder_(this.data.id).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: '已取消回收'
        })
        this.setData({
          confirm: false
        })
        this.init();
      } else {
        wx.showToast({
          title: res.message,
        })
      }
    })
  },

  cancelAcceptOrder() {
    cancelAcceptOrder_(this.data.id).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: '已取消接单'
        })
        wx.redirectTo({
          url: '/pages/myOrder/myOrder',
        })
      } else {
        wx.showToast({
          title: res.message,
        })
      }
    })
  },

  collectOrder() {
    wx.navigateTo({
      url: '/pages/addDetail/addDetail?id=' + this.data.id,
    })
  },


  async init() {
    wx.showLoading({
      title: '加载中...',
    })
    const userResult = await authInfo();
    if (userResult.code == 200) {
      this.setData({
        curUserId: userResult.data.user.id,
      })
    }

    const res = await orderDetail(this.data.id);
    if (res.code != 200) {
      console.log(res.message);
    } else {
      const oo = res.data.order;
      const em = res.data.employer;
      const co = res.data.collector;
      this.setData({
        details: res.data.details.map(e => {
          return {
            weight: util.gToKg(e.weight),
            amount: util.fenToYuan(e.amount),
            itemName: e.itemName,
            categoryName: e.categoryName
          }
        }),
        id: oo.id,
        state: oo.state,
        stateName: util.getOrderState(oo.state),
        collectorName: co === null ? '暂无':co.name,
        collectorPhone: co === null ? '暂无':co.phone,
        employerName: em === null ? '暂无':em.name,
        employerPhone: em === null ? '暂无':em.phone,
        amount: oo.amount === null ? '暂无':util.fenToYuan(oo.amount) + '￥',
        totalWeight: oo.totalWeight === null ? '暂无':util.gToKg(oo.totalWeight) + 'Kg',
        address: oo.address,
        remark: oo.remark,
        subscriptionTime: oo.subscriptionTime,
        acceptTime: oo.acceptTime === null ? '暂无':oo.acceptTime,
        createTime: oo.createTime,
        finishTime: oo.finishTime === null ? '暂无':oo.finishTime,
        longitude: oo.longitude,
        latitude: oo.latitude
      })

      if (this.data.details === null || this.data.details.length === 0) {
        this.setData({
          detailValue: "暂无"
        })
      }

      // 订单状态为待接单且当前用户为发布者，则可取消订单
      if (oo.state === 0 && oo.employerId === this.data.curUserId) {
          this.setData({
            cancel: true
          })
      }

      // 订单状态为待回收且当前用户为订单回收员，则可取消接单
      if (oo.state === 1 && oo.collectorId === this.data.curUserId) {
        this.setData({
          cancelAccept: true
        })
      }

      // 订单状态为待回收且当前用户为订单回收员，则可完成回收
      if (oo.state === 1 && oo.collectorId === this.data.curUserId) {
        this.setData({
          collect: true
        })
      }

      // 订单状态为已回收且当前用户为发布者，则可确认完成
      if (oo.state === 2 && oo.employerId === this.data.curUserId) {
        this.setData({
          confirm: true
        })
      }
    }
    wx.hideLoading({
      success: (res) => { },
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
      this.setData({
        id: options.id
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})